<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class RoomScore extends Base
{
    protected $table_room_score = "room_score"; //房间积分表
    protected $table_room_score_log = "room_score_log"; //房间增减分日志
    const DEFAULT_REPUTATION = 100; //默认分数

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 根据房间id，读取一个房间的信誉分
     * @param $roomId int
     * @return int
     */
    public function getReputationByRoomId($roomId)
    {
        $res = DB::table($this->table_room_score)
            ->select(['reputation'])
            ->where('room_id', $roomId)
            ->first();
        return $res === null ? self::DEFAULT_REPUTATION : $res->reputation;
    }

    /**
     * 根据房间id列表，读取房间信誉分
     * @param $roomIds array
     * @return array
     */
    public function getReputationByRoomIds($roomIds)
    {
        $res = DB::table($this->table_room_score)
            ->select(['room_id', 'reputation'])
            ->whereIn('room_id', $roomIds)
            ->get();
        $create = [];
        if (count($res) > 0) {
            $res = $this->objToArray($res);
            $create = array_column($res, 'reputation', 'room_id');
        }
        return $create;
    }

    /**
     * 增减房间信誉分
     * @param $opt int 操作类型：1增加;2减少
     * @param $roomId int 房间id
     * @param $score int 分数
     * @return int
     */
    public function setRoomReputation($opt, $roomId, $score)
    {
        if (empty($score)) {
            return 1;
        }

        $where = ['room_id' => $roomId];
        $res = 0;
        $count = DB::table($this->table_room_score)->where($where)->count();
        if (empty($count)) {
            if ($opt == 1) {
                $score = self::DEFAULT_REPUTATION + $score; //增加
            } else {
                $score = self::DEFAULT_REPUTATION - $score; //减少
            }
            $data = [
                'room_id' => $roomId,
                'reputation' => $score,
            ];
            $res = DB::table($this->table_room_score)->insert($data);
        } else {
            if ($opt == 1) {
                $res = DB::table($this->table_room_score)->where($where)->increment('reputation', $score);
            } elseif ($opt == 2) {
                $res = DB::table($this->table_room_score)->where($where)->decrement('reputation', $score);
            }
        }

        return intval($res);
    }


    /**
     * 删除房间信誉分记录（还原到默认分数）
     * @param $roomId int 房间id
     * @return int
     */
    public function delRoomReputation($roomId)
    {
        $where[] = ['room_id', '=', $roomId];

        $result = DB::table($this->table_room_score)->where(["room_id" => $roomId])->delete();

        return $result;
    }

    /**
     * 增加房间增减分日志
     * @param $type int 积分类型 1:房间信誉分
     * @param $roomId int 房间id
     * @param $score int 分数
     * @param $desc string 描述
     * @param $adminName string 操作人
     * @return int
     */
    public function addRoomScoreLog($type, $roomId, $score, $desc = "", $adminName = "")
    {
        $data = [
            'room_id' => $roomId,
            'type' => $type,
            'score' => $score,
            'desc' => $desc,
            'created_admin' => $adminName,
        ];
        $res = DB::table($this->table_room_score_log)->insert($data);
        return intval($res);
    }

    /**
     * 读取积分范围内的数据
     * @param $roomIds
     * @return array
     */
    public function getReputationByRange($start, $end)
    {
        $res = DB::table($this->table_room_score)
            ->select(['room_id', 'reputation'])
            ->whereBetween('reputation', [$start, $end])
            ->get();
        $create = [];
        if (count($res) > 0) {
            $res = $this->objToArray($res);
            $create = array_column($res, 'reputation', 'room_id');
        }
        return $create;
    }

    /**
     * 读取每个房间最新的一条增减记录
     * @param $roomIds
     * @return array
     */
    public function getReputationLastLog($roomIds)
    {
        $where[] = ['type', 1];
        $idsRes = DB::table($this->table_room_score_log)
            ->select([DB::raw("max(id) as max")])
            ->where($where)
            ->whereIn('room_id', $roomIds)
            ->groupBy('room_id')
            ->get();
        $idsRes = $this->objToArray($idsRes);

        $ids = array_column($idsRes, 'max');
        $res = DB::table($this->table_room_score_log)
            ->select(['room_id', 'type', 'score', 'created_at'])
            ->whereIn('id', $ids)
            ->get();
        return $this->objToArray($res);
    }

}
